package com.wanmao.test.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.wanmao.test.bean.YDBean;
import com.wanmao.test.dbhelper.DBOpenHelper;

import java.util.ArrayList;
import java.util.List;



public class YDService {
    private DBOpenHelper dBOpenHelper;

    public YDService(Context context) {
        this.dBOpenHelper = new DBOpenHelper(context);
    }

    public void save(YDBean bean) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        db.insert("YD_TABLE", null, settingValue(bean));
    }


    private ContentValues settingValue(YDBean bean) {
        ContentValues values = new ContentValues();
        values.put("YD_ID", bean.getYD_ID());
        values.put("XMID", bean.getXMID());
        values.put("XZID", bean.getXZID());
        values.put("YDMC", bean.getYDMC());
        values.put("WTMS", bean.getWTMS());
        values.put("JCJG", bean.getJCJG());
        values.put("PICTURE", bean.getPICTURE());
        values.put("SBLX", bean.getSBLX());
        values.put("OBJ_ID", bean.getOBJ_ID());
        values.put("RWID", bean.getRWID());
        return values;
    }

    public List<YDBean> findXMID(String XMid) {
        String sql = "select * from YD_TABLE where XMID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, new String[]{XMid});
      return toListBean(cursor);

    }
    public List<YDBean> findXMID(String XMid,String RWID) {
        String sql = "select * from YD_TABLE where XMID=?  and RWID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, new String[]{XMid,RWID});

        return toListBean(cursor);

    }


    public List<YDBean> findXZID(String XZid) {
        String sql = "select * from YD_TABLE where XZID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, new String[]{XZid});

        return toListBean(cursor);

    }



    private List<YDBean> toListBean(Cursor cursor) {
        List<YDBean> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            YDBean bean = new YDBean();
            bean.setXMID(cursor.getString(cursor.getColumnIndex("XMID")));
            bean.setXZID(cursor.getString(cursor.getColumnIndex("XZID")));
            bean.setYD_ID(cursor.getString(cursor.getColumnIndex("YD_ID")));
            bean.setYDMC(cursor.getString(cursor.getColumnIndex("YDMC")));
            bean.setWTMS(cursor.getString(cursor.getColumnIndex("WTMS")));
            bean.setJCJG(cursor.getString(cursor.getColumnIndex("JCJG")));
            bean.setPICTURE(cursor.getString(cursor.getColumnIndex("PICTURE")));
            bean.setSBLX(cursor.getString(cursor.getColumnIndex("SBLX")));
            bean.setOBJ_ID(cursor.getString(cursor.getColumnIndex("OBJ_ID")));
            bean.setRWID(cursor.getString(cursor.getColumnIndex("RWID")));
            list.add(bean);
        }

        return list;
    }
    public YDBean updateYDJCJG(String OBJ_ID, String WTMS,String JCJG,String PICTURE,String SBLX) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        String sql="update YD_TABLE set WTMS='"+WTMS+"',JCJG='"+JCJG+"',PICTURE='"+PICTURE+"',SBLX='"+SBLX+"' where OBJ_ID='"+OBJ_ID+"'";
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor.moveToNext()){
            return toBean(cursor);
        }

        return null;
    }


    public YDBean updateYDisENd(String YDID, String WTMS,String JCJG,String PICTURE,String SBLX) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        String sql="update YD_TABLE set WTMS='"+WTMS+"',JCJG='"+JCJG+"',PICTURE='"+PICTURE+"',SBLX='"+SBLX+"' where YD_ID='"+YDID+"'";
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor.moveToNext()){
            return toBean(cursor);
        }

        return null;
    }

    public YDBean findYDByOBJID(String OBJ_ID) {
        String sql = "select * from YD_TABLE where OBJ_ID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, new String[]{OBJ_ID});

        return toBean(cursor);

    }
    private YDBean toBean(Cursor cursor) {
        YDBean bean = new YDBean();
        while (cursor.moveToNext()) {
            bean.setXMID(cursor.getString(cursor.getColumnIndex("XMID")));
            bean.setXZID(cursor.getString(cursor.getColumnIndex("XZID")));
            bean.setYD_ID(cursor.getString(cursor.getColumnIndex("YD_ID")));
            bean.setYDMC(cursor.getString(cursor.getColumnIndex("YDMC")));
            bean.setWTMS(cursor.getString(cursor.getColumnIndex("WTMS")));
            bean.setJCJG(cursor.getString(cursor.getColumnIndex("JCJG")));
            bean.setPICTURE(cursor.getString(cursor.getColumnIndex("PICTURE")));
            bean.setSBLX(cursor.getString(cursor.getColumnIndex("SBLX")));
            bean.setISEND(cursor.getString(cursor.getColumnIndex("ISEND")));
            bean.setOBJ_ID(cursor.getString(cursor.getColumnIndex("OBJ_ID")));
            bean.setRWID(cursor.getString(cursor.getColumnIndex("RWID")));
        }
        return bean;
    }
}
